<?php
/**  
* @var date1日期1 
* @var date2 日期2
* @var tags 年月日之间的分隔符标记,默认为'-'  
* @return 相差的月份数量 
* @example: $date1 = "2003-08-11"; $date2 = "2008-11-06"; $monthNum = getMonthNum( $date1 , $date2 ); echo $monthNum; 
*/
function getMonthNum( $date1, $date2, $tags='-' ){  
	$date1 = explode($tags,$date1);  
	$date2 = explode($tags,$date2);
	return abs(($date2[0] - $date1[0]) * 12 + ($date2[1] - $date1[1])); 
}
//相差天数
//start_time  开始时间（时间戳）
//end_time  结束时间（时间戳）
function count_days($start_time,$end_time){
	return round(abs($end_time-$start_time)/86400);
}

function mobile($mobile){
    $pattern="/^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^16[6,8]{1}\d{8}$|^17[0,3,6,7,8]{1}\d{8}$|^18[\d]{9}$/";
    if(preg_match($pattern,$mobile)){
        return true;
    }
    return false;
}
/**
 * 字符串截取，支持中文和其他编码
 * @static
 * @access public
 * @param string $str 需要转换的字符串
 * @param string $start 开始位置
 * @param string $length 截取长度
 * @param string $charset 编码格式
 * @param string $suffix 截断显示字符
 * @return string
 */
function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true) {
    if(function_exists("mb_substr"))
        $slice = mb_substr($str, $start, $length, $charset);
    elseif(function_exists('iconv_substr')) {
        $slice = iconv_substr($str,$start,$length,$charset);
        if(false === $slice) {
            $slice = '';
        }
    }else{
        $re['utf-8']   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
        $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
        $re['gbk']    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
        $re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
        preg_match_all($re[$charset], $str, $match);
        $slice = join("",array_slice($match[0], $start, $length));
    }
    return $suffix ? $slice.'...' : $slice;
}
//文字水印
//$text  水印文字
//$file_name 图片路径
function textgd($file_name,$text='仅限海关使用'){
		$image = new \Think\Image(); // 在图片右下角添加水印文字 ThinkPHP 并保存为new.jpg
		$image->open($file_name)->text($text,'./ThinkPHP/Library/Think/Verify/ttfs/msyh.ttf',60,'#E1E1E1',\Think\Image::IMAGE_WATER_EAST,0,45)->save($file_name); 
}
//数据库是否存在
function has_db($dbName){
	$Model = M();
	$sql_head="SELECT * FROM information_schema.SCHEMATA where SCHEMA_NAME='$dbName';";
	$ress = $Model->query($sql_head);
	if(!empty($ress)){
		// alert(0,'数据库已存在');
		return 1;
	}else{
		return 0;
	}
}
//返回json
function alert_json($status,$message){
	header('Content-Type:application/json; charset=utf-8');
	$return['message']=$message;
	$return['status']=$status;
	exit(json_encode($return,0));
}
//电话验证,形如(xxxx-xxxxxxxx)
function isTel($tel){
	$isTel="/^([0-9]{3,4}-)?[0-9]{7,8}$/";
	if(preg_match($isTel,$tel)){
		return 1;
	}else{
		return 0;
	}
}
//省列表
// function province_list(){
	// $region=M('Region');
	// $list=$region->where(array('type'=>1))->select();
	// return $list;
// }
function province_list(){
	$region=M('Region');
	$list=$region->where(array('type'=>1))->select();
	return $list;
}
//市列表
// function city_list($parentCode){
	// $region=M('Region');
	// $list=$region->where(array('type'=>2,'parentCode'=>$parentCode))->select();
	// return $list;
// }
function city_list($name){
	$region=M('Region');
	$info=$region->where(array('type'=>1,'fullName'=>array('like',"%".$name."%")))->find();
	$list=$region->where(array('type'=>2,'parentCode'=>$info['code']))->select();
	return $list;
}
//区/县列表
// function area_list($parentCode){
	// $region=M('Region');
	// $list=$region->where(array('type'=>3,'parentCode'=>$parentCode))->select();
	// return $list;
// }
function area_list($province_name,$city_name){
	$region=M('Region');
	$info_province=$region->where(array('type'=>1,'fullName'=>array('like',"%".$province_name."%")))->find();
	// echo $region->getlastsql();
	$info_city=$region->where(array('parentCode'=>$info_province['code'],'type'=>2,'fullName'=>$city_name))->find();
	// echo $region->getlastsql();
	$list=$region->where(array('type'=>3,'parentCode'=>$info_city['code']))->select();
	// echo $region->getlastsql();
	return $list;
}
//图片
function get_pic($pic){
	$file_url=$pic;
	if(!empty($file_url)){
		return __ROOT__.'/'.$file_url;
	}else{
		return __ROOT__.'/nopic.gif';
	}
}

//仓库名称
function get_warehousename($warehouse_code){
	$warehouse=M('Warehouse');
	$warehouse_info=$warehouse->where(array('warehouse_code'=>$warehouse_code))->find();
	return $warehouse_info['name'];
}
//二维数组值匹配
//$value:要匹配的值
//$array:要匹配的数组
function deep_in_array($value,$array) {   
	foreach($array as $item){
		if(!is_array($item)){
			if($item == $value){
				return true;  
			}else{
				continue;   
			}  
		}
		if(in_array($value, $item)) {  
			return true;      
		} else if(deep_in_array($value, $item)) {  
			return true;      
		}  
	}   
	return false;   
}
